package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.RoomCate;
import com.scu.hotelsystem.service.RoomCateService;
import com.scu.hotelsystem.util.PageInfo;
import com.scu.hotelsystem.util.Result;
import com.scu.hotelsystem.util.search.RoomCateSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class RoomCateController {
    @Autowired
    private RoomCateService roomCateService;

    @RequestMapping("/roomCateListPage")
    public String roomCateListPage(){
        return "roomcatelist";
    }

    @RequestMapping("/searchRoomCateListRest")
    @ResponseBody
    public Result searchRoomCateListRest(RoomCateSearch roomCateSearch, PageInfo pageInfo){
        List<RoomCate> roomCateList;
        RoomCate search;
        int count;

        search = new RoomCate(roomCateSearch);
        roomCateList = roomCateService.searchRoomCateList(search, pageInfo.index(), pageInfo.limit());
        count = roomCateList.size();

        return new Result(count, roomCateList);
    }

    @RequestMapping("/roomCateEditPage")
    public ModelAndView roomCateEditPage(RoomCateSearch roomCateSearch){
        List<RoomCate> roomCateList;
        ModelAndView view;

        view = new ModelAndView("roomcateedit");
        RoomCate search = new RoomCate(roomCateSearch);
        roomCateList = roomCateService.searchRoomCateList(search, 0, 1);
        if (roomCateList.size() != 0){
            view.addObject("roomCate", roomCateList.get(0));
        }

        return view;
    }

    @RequestMapping("/updateRoomCate")
    @ResponseBody
    public Result updateRoomCate(RoomCate roomCate){
        int n;
        n = roomCateService.updateRoomCate(roomCate);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/roomCateAddPage")
    public String roomCateAddPage(){
        return "roomcateadd";
    }

    @RequestMapping("/addRoomCate")
    @ResponseBody
    public Result addRoomCate(RoomCate roomCate){
        int n;
        n = roomCateService.addRoomCate(roomCate);

        if (n == 1 || n == 2){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/deleteRoomCate")
    @ResponseBody
    public Result deleteRoomCate(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = roomCateService.deleteRoomCate(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }
    }
}
